<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :title="actionStatus == 'edit' ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="18" :sm="24" :xs="24">
          <a-form-item label="支付方式名称" v-bind="validateInfos.code">
            <a-select
              optionFilterProp="label"
              :options="options"
              show-search
              allow-clear
              autocomplete="off"
              placeholder="请选择"
              v-model:value="form.code"
              @select="change"
            />
          </a-form-item>
          <a-form-item label="状态">
            <a-switch v-model:checked="checked" @change="changeStatus" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { get_detail, get_pay_type } from '@/api/setting/pay_type';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    editDataId: [String, Number],
    storeId: [String, Number]
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  const checked = ref(true);
  const options = ref([]);

  onMounted(() => {
    form.id = props.editDataId;
    form.store_code = props.storeId;
    options.value = [];
    get_pay_type().then((res) => {
      console.log('res==', res);
      Object.keys(res).forEach((key) => {
        options.value.push({
          label: res[key],
          value: key
        });
      });
    });
    if (props.editDataId) {
      get_detail({ id: props.editDataId }).then((res) => {
        form.store_code = res.store_code;
        form.name = res.name;
        form.code = res.code;
        form.status = res.status;
        checked.value = res.status == 1 ? true : false;
      });
    }
  });
  const changeStatus = (e) => {
    form.status = e ? '1' : '0';
  };
  const change = (e) => {
    options.value.forEach((item) => {
      if (item.value == e) {
        form.name = item.label;
      }
    });
  };

  // 表单数据
  const form = reactive({
    id: '',
    store_code: '',
    name: '',
    code: null,
    status: 1
  });
  // 表单验证规则
  const rules = reactive({
    code: [
      {
        required: true,
        message: '请选择支付方式',
        type: 'string',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
